package com.xigege.netty.simpleExample.basic;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

/**
 * @author huangxi
 * @class HelloServer
 * @description HelloServer
 * @create 2022-08-19 15:23
 */
public class HelloServer {
    public static void main(String[] args) {
        // 配置整个 Netty 程序，串联各个组件
        new ServerBootstrap()
                // 用来设置一个 EventLoop
                .group(new NioEventLoopGroup())
                // 选择服务器的ServerSocketChannel实现
                .channel(NioServerSocketChannel.class)
                // 设置业务处理类（自定义的 handler)，创建一个通道初始化对象，在workGroup生效
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    // 初始化channel
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        // 添加处理器
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(msg);
                            }
                        });
                    }
                    //绑定端口
                }).bind(6666);

    }
}
